package com.wld.xm.framework.core.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class QiNiuUploadThread extends Thread {
    private List<FileInputStream> uploadFileInputStreams;
    private List<String> fileKeys;
    /**
     * 测试七牛云配置
     */
    private String qiniu_access_key;
    private String qiniu_secret_key;
    private String qiniu_bucket;
    private String localTempDir;

    public QiNiuUploadThread(List<FileInputStream> uploadFileInputStreams, List<String> fileKeys, String qiniu_access_key, String qiniu_secret_key, String qiniu_bucket) {
        this.uploadFileInputStreams = uploadFileInputStreams;
        this.fileKeys = fileKeys;
        this.qiniu_access_key = qiniu_access_key;
        this.qiniu_secret_key = qiniu_secret_key;
        this.qiniu_bucket = qiniu_bucket;
        this.localTempDir = Paths.get(System.getenv("java.io.tmpdir"), qiniu_bucket).toString();
    }

    public QiNiuUploadThread(FileInputStream fileInputStream, String fileKey, String qiniu_access_key, String qiniu_secret_key, String qiniu_bucket) {
        if (null == uploadFileInputStreams) uploadFileInputStreams = new ArrayList<>();
        if (null == fileKeys) fileKeys = new ArrayList<>();
        uploadFileInputStreams.add(fileInputStream);
        fileKeys.add(fileKey);
        this.qiniu_access_key = qiniu_access_key;
        this.qiniu_secret_key = qiniu_secret_key;
        this.qiniu_bucket = qiniu_bucket;
        this.localTempDir = Paths.get(System.getenv("java.io.tmpdir"), qiniu_bucket).toString();
    }


    @Override
    public void run() {
        if (null != uploadFileInputStreams && !uploadFileInputStreams.isEmpty() && null != fileKeys && !fileKeys.isEmpty()) {
            // 构造一个带指定Zone对象的配置类
            Configuration cfg = new Configuration(Zone.zone0());
            //设置断点续传文件进度保存目录
            try {
                FileRecorder fileRecorder = new FileRecorder(localTempDir);
                // ...其他参数参考类注释
                UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
                // ...生成上传凭证，然后准备上传
                String accessKey = qiniu_access_key;
                String secretKey = qiniu_secret_key;
                String bucket = qiniu_bucket;
                // 默认不指定key的情况下，以文件内容的hash值作为文件名
                for (FileInputStream uploadFileInputStream : uploadFileInputStreams) {
                    int index = uploadFileInputStreams.indexOf(uploadFileInputStream);
                    if (index < fileKeys.size()) {
                        Auth auth = Auth.create(accessKey, secretKey);
                        String upToken = auth.uploadToken(bucket);
                        try {
                            Response response = uploadManager.put(uploadFileInputStream, fileKeys.get(index), upToken, null, null);
                            // 解析上传成功的结果
                            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                        } catch (QiniuException ex) {
                            Response r = ex.response;
                            throw ex;
                        }
                    }
                }
            } catch (IOException e) {
                try {
                    throw e;
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

}
